package com.csun.action;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.csun.base.BaseAction;
import com.csun.entity.Role;
import com.csun.service.RoleService;
import com.csun.util.ResponseKit;

/**
 * @ClassName: RoleAction.java
 * @Description:角色控制核心类
 * @author danjinsong danjingsong@dragonwake.cn
 * @date 2017年2月16日下午3:40:14
 */
@SuppressWarnings("serial")
@Controller
public class RoleAction extends BaseAction{

	private Role role;									   //角色				
	
	private String errorMsg;							   //错误信息

	@Autowired
	private RoleService roleService;
	//=============从此处开始为业务逻辑方法 ========================//
	/**
	 * ajax请求角色列表方法
	 * @return
	 */
	public String allList(){
		List<Role> rolelist = roleService.find(null, null); //加载全部角色
		JsonConfig config = new JsonConfig();				//配置转化规则
		config.setExcludes(
				new String[]{"sonRoles","userList"});		//忽略无关属性
		JSONArray list = 
				JSONArray.fromObject(rolelist, config);		//json转化数组
		JSONObject json = new JSONObject();
		json.put("success", true);							//设置成功标识符
		json.put("list", list);
		ResponseKit.write(getresponse(), json); 			//写出json
		return null;
	}
	
	/**
	 * 保存角色
	 * @return
	 */
	public String save(){
		if(role != null){
			roleService.save(role);
			ResponseKit.write(getresponse(), "success");	//添加成功
		}
		else{
			ResponseKit.write(getresponse(), "error");		//添加失败
		}
		return null;
	}
	//=============从此处开始为中间调用方法 =========================//
	
	
		 
		
	//=============从此处开始为 Get Set方法=======================//
	public Role getRole() {
		return role;
	}
	public void setRole(Role role) {
		this.role = role;
	}
	public String getErrorMsg() {
		return errorMsg;
	}
	public void setErrorMsg(String errorMsg) {
		this.errorMsg = errorMsg;
	}
}
